package Competition;

import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: Competition
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-10-29  10:30
 * @Description: TODO
 * @Version: 1.0
 */
public class 找出数组中的K值 {

    public int findKOr(int[] nums, int k) {
        String[] array = new String[nums.length];
        int len = 0;
        for (int i = 0; i < nums.length; i++) {
            array[i] = new StringBuffer(Integer.toBinaryString(nums[i])).reverse().toString();
            len = Math.max(len, array[i].length());
        }
        HashMap<Integer, List<String>> map = new HashMap<>();
        for (int i = 0; i <= len; i++) {
            List<String> list = new ArrayList<>();
            for (int j = 0; j < array.length; j++) {
                if (array[j].length() > i && array[j].charAt(i) == '1') {
                    list.add(array[j]);
                }
            }
            map.put(i, list);
        }
        int result = 0;
        for (int i : map.keySet()) {
            if (map.get(i).size() >= k) {
                result += Math.pow(2, i);
            }
        }
        return result;
    }

    @Test
    public void test() {
        int kOr = findKOr(new int[]{2,12,1,11,4,5}, 6);
        System.out.println(kOr);
    }
}
